package com.unlcn.ils.kas.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.plugins.Page;
import com.unlcn.ils.kas.dao.model.sysadmin.User;
import com.unlcn.ils.kas.dao.model.sysadmin.UserRole;
import com.unlcn.ils.kas.dao.model.sysadmin.bo.UserBo;
import com.unlcn.ils.kas.service.sysadmin.UserService;
import com.unlcn.ils.kas.vo.RestVO;
import com.unlcn.ils.kas.vo.UserRoleRequestVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Nicky on 17/11/24.
 */

@Controller
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/queryPage", consumes = "application/json")
    @ResponseBody
    public RestVO<Page<UserBo>> queryPage(@RequestBody Page<UserBo> page) {
        RestVO<Page<UserBo>> userBoResponseBO = new RestVO<>(0, "success");
        Page<UserBo> userBo = userService.queryPageUser(page);
        userBoResponseBO.setData(userBo);
        return userBoResponseBO;
    }

    @PostMapping(value = "/create", consumes = "application/json")
    @ResponseBody
    public RestVO<User> create(@RequestBody User user) {
        RestVO<User> response = new RestVO<>(0, "success");
        try {
            response.setData(userService.addUser(user));
        } catch (Exception e) {
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;

    }

    @PostMapping(value = "/update", consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> update(@RequestBody User user) {
        RestVO<Integer> response = new RestVO<>(0, "success");
        try {
            response.setData(userService.updateUser(user));
        } catch (Exception e) {
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;

    }

    @PostMapping(value = "/delete", consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> delete(@RequestBody List<Integer> userIds) {
        RestVO<Integer> response = new RestVO<>(0, "success");
        try {
            response.setData(userService.deleteUser(userIds));
        } catch (Exception e) {
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @GetMapping(value = "/ownRole")
    @ResponseBody
    public RestVO<List<Integer>> listOwnRoleIds(@RequestParam("userId") Integer userId) {
        RestVO<List<Integer>> response = new RestVO<>(0, "success");
        try {
            List<UserRole> userRoleList = userService.listUserRole(userId);
            if (!Objects.equals(userRoleList, null)) {
                List<Integer> roleIds = new ArrayList<>();
                for (UserRole userRole : userRoleList) {
                    roleIds.add(userRole.getRoleId());
                }
                response.setData(roleIds);
            }
        } catch (Exception e) {
            response.setCode(10001);
            response.setMessage(e.getMessage());

        }
        return response;
    }

    // 赋予角色 zgx
    @PostMapping(value = "/grantRole", consumes = "application/json")
    @ResponseBody
    public RestVO<Integer> grantRole(@RequestBody UserRoleRequestVO reVO) {
        RestVO<Integer> result = new RestVO<>(0, "success");
        try {
            int i = userService.assignRole(reVO.getUserId(), reVO.getRoleIds());
            result.setData(i);
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @GetMapping(value = "/listUsers")
    @ResponseBody
    public RestVO<List<UserBo>> listUsers() {
        RestVO<List<UserBo>> response = new RestVO<>(0, "success");
        try {
            response.setData(userService.listUsers());
        } catch (Exception e) {
            response.setCode(10001);
            response.setMessage(e.getMessage());
        }
        return response;
    }
}
